<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="所属分类" prop="cid">
				<el-select v-model="form.cid"  filterable style="width: 100%" clearable>
					<el-option v-for="item in cat" :key="item.cid" :label="item.name" :value="item.cid"/>
				</el-select>
			</el-form-item>
			<el-form-item label="封面图" prop="feng_thumb">
				<sc-upload v-model="form.feng_thumb" title="上传封面图"></sc-upload>
			</el-form-item>
			<el-form-item label="商品名称" prop="title">
				<el-input v-model="form.title" clearable placeholder="必填"></el-input>
			</el-form-item>
            <el-form-item label="商品多规格" prop="guige">
				<el-switch v-model="guige"></el-switch>
			</el-form-item>
			<el-form-item label="" prop="" v-if="guige">
					<el-card shadow="never" style="margin-top: 10px;margin-bottom: 10px;width: 100%;" id="card">
					    <el-table :data="guigelist" border style="width: 100%;margin-top: 5px;">
					    <el-table-column label="规格名称" align="center">
					         <template #default="scope">
					             <el-input placeholder="输入名称" v-model="scope.row.can"></el-input>
					         </template>
					    </el-table-column>
					    <el-table-column label="价格" width="120" align="center">
					        <template #default="scope">
					             <el-input placeholder="输入价格" v-model="scope.row.pro_price"></el-input>
					         </template>
					    </el-table-column>
					    <el-table-column label="原价" width="120" align="center">
					        <template #default="scope">
					             <el-input placeholder="非必填" v-model="scope.row.pro_original_price"></el-input>
					         </template>
					    </el-table-column>
					    <el-table-column  label="库存" width="120" align="center">
					        <template #default="scope">
					             <el-input placeholder="输入库存" v-model="scope.row.pro_stock" type="number"></el-input>
					         </template>
					    </el-table-column>
					    <el-table-column label="操作" width="80" align="center">
					            <template #default="scope">
					                <el-button size="small" type="info" @click="delguige(scope.$index)" v-if="scope.$index>0">删除</el-button>
					                <el-button size="small" type="primary" @click="addguige" v-if="scope.$index==0">添加</el-button>
					            </template>
					    </el-table-column>
					    </el-table>
					</el-card>

			</el-form-item>

            <el-form-item label="价格" prop="price" v-if="!guige">
				<el-input v-model="form.price" class="ipt">
				    <template v-slot:append>元</template>
				</el-input>
			</el-form-item>
			<el-form-item label="原价" prop="origin_price" v-if="!guige">
				<el-input v-model="form.origin_price" class="ipt">
				    <template v-slot:append>元</template>
				</el-input>
			</el-form-item>
            <el-form-item label="库存" prop="stock" v-if="!guige">
				<el-input-number v-model="form.stock" clearable></el-input-number>
			</el-form-item>
            <el-form-item label="描述" prop="des">
				<el-input v-model="form.des" clearable></el-input>
			</el-form-item>
            <el-form-item label="标签" prop="tags">
				<el-input v-model="form.tags" clearable placeholder="多个标签#隔开"></el-input>
			</el-form-item>
			<el-form-item label="编码" prop="code">
				<el-input v-model="form.code" clearable></el-input>
			</el-form-item>
			<el-form-item label="下架" prop="status">
				<el-switch v-model="form.status" active-value="0" inactive-value="1"></el-switch>
			</el-form-item>
            <el-form-item label="置顶" prop="is_top">
				<el-switch v-model="form.is_top" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
            <el-form-item label="推荐" prop="is_tj">
				<el-switch v-model="form.is_tj" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="详细" prop="content">
				<el-card shadow="never">
					<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="400"></sc-editor>
				</el-card>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
    import { defineAsyncComponent } from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		components: {
			scEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				cat:[],
                guige:false,
                guigelist:[
                    {'can':'','pro_price':'','pro_original_price':'','pro_stock':''}
                ],
				mode: "add",
				titleMap: {
					add: '新增商品',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					stock:999
				},
				//验证规则
				rules: {

					title: [
						{required: true, message: '请输入标题'}
					]
				}
			}
		},
		mounted() {
			 this.getcat()
		},
		methods: {
			async getcat(){
				var res = await this.$API.system.goods.cat.get();
				if(res.code == 200){
					 	this.cat = res.data.rows
				}
			},
			async delSpec(id,dex){
				var reqData = {id:id,'act':'del'}
				//var res = await this.$API.demo.goodsspec_del.post(reqData);
				var res = await this.$API.system.universal.handle.post(reqData,'/goods/goodsspec_del');
				if(res.code == 200){
					this.$message.success("删除成功")
					this.guigelist.splice(dex,1)
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
            addguige(){
                this.guigelist.push( {'can':'','pro_price':'','pro_original_price':'','pro_stock':''})
            },
            delguige(dex){
				if(this.guigelist[dex].id=='' || this.guigelist[dex].id == null){
					 this.guigelist.splice(dex,1)
				}else{
					 this.delSpec(this.guigelist[dex].id,dex)
				}

            },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
                        this.form.guige = this.guige
                        this.form.guigedata = this.guigelist

						//var res = await this.$API.demo.goods_post.post(this.form);
						var res = await this.$API.system.universal.handle.post(this.form,'/goods/goods_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				if(this.mode == 'edit'){
					 this.guigelist = data.guigedata
					 this.guige = data.guige==1?true:false
				}
				this.form = data
			}
		}
	}
</script>

<style>
#card .el-input__inner{
	text-align: center;
}
</style>
